package com.tanhua.dubbo.api;

import cn.hutool.core.date.DateUtil;
import com.tanhua.dubbo.api.mongo.VisitorApi;
import com.tanhua.model.mongo.Visitors;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 访客服务接口实现类
 */
@DubboService
@Slf4j
public class VisitorApiImpl implements VisitorApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 记录访客数据
     *
     * @param visitors
     */
    @Override
    public void add(Visitors visitors) {
        log.debug("*************visitors**{}******", visitors);
        Date date = new Date();
        String nowDay = DateUtil.format(date, "yyyyMMdd");//20220319
        long nowTime = date.getTime();//当前时间
        //1.查询当前登录用户是否今天已经访问了此用户
        Query query = new Query();
        query.addCriteria(
                Criteria.where("userId").is(visitors.getUserId())
                        .and("visitorUserId").is(visitors.getVisitorUserId())
                        .and("visitDate").is(nowDay)  // yyyyMMdd
        );
        if (mongoTemplate.exists(query, Visitors.class)) {
            log.debug("*************updateFirst**{}******");
            //2.如果已经访客 更新数据
            Update update = new Update();
            update.set("from", visitors.getFrom());//from
            update.set("date", nowTime);//来访时间
            mongoTemplate.updateFirst(query, update, Visitors.class);
        } else {
            log.debug("*************insert**{}******");
            //3.如果没有访问，保存数据
            visitors.setDate(nowTime);//来访时间年月日时分秒 long
            visitors.setVisitDate(nowDay);//yyyyMMdd
            mongoTemplate.insert(visitors);
        }
    }

    /**
     * 谁看过我
     *
     * @param timeLong
     * @param loginUserId
     * @return
     */
    @Override
    public List<Visitors> findVisitors(Long timeLong, Long loginUserId) {
        List<Visitors> visitorsList = new ArrayList<>();
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(loginUserId));
        query.with(Sort.by(Sort.Direction.DESC,"date"));
        query.limit(4);
        if (timeLong != null) {
            log.debug("******has timeLong*******",timeLong);
            query.addCriteria(Criteria.where("date").gt(timeLong));
            //1根据当前用户id和读取消息时间查询访客表 limit 4条 (date降序)
            visitorsList = mongoTemplate.find(query, Visitors.class);
        }else {
            log.debug("******no timeLong*******");
            //2根据当前用户id 查询访客表 limit 4条  (date降序)
            visitorsList = mongoTemplate.find(query, Visitors.class);
        }
        return visitorsList;
    }
}
